package com.gm.wholesale.module.report.ui;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.gm.commonlib.base.baseBean.BaseFragmentInfo;
import com.gm.commonlib.base.baseui.baseFragment.BaseFragment;
import com.gm.commonlib.module.evenbus.EventMessage;
import com.gm.wholesale.R;
import com.gm.wholesale.module.order.adapter.ViewPagerAdapter;
import com.gm.wholesale.module.order.listener.MyOnPageChangeListener;
import com.gm.wholesale.module.report.ui.tab.CustormReportFragment;
import com.gm.wholesale.module.report.ui.tab.GoodReportFragment;
import com.gm.wholesale.module.report.ui.tab.SaleReportFragment;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class ReportMangerFragment extends BaseFragment {
    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    public ViewPagerAdapter mAdapter;
    private SaleReportFragment mSaleReportFragment;
    private GoodReportFragment mGoodReportFragment;
    private CustormReportFragment mCustormReportFragment;

    private BaseFragment mCurrentFragment;
    @Override
    protected void onCreateContentBody(ViewGroup parent) {
        super.onCreateContentBody(parent);
        View view= LayoutInflater.from(context).inflate(R.layout.fragment_report_manger, parent);
        initView(view);
    }

    @Override
    public void onReceiveEvent(EventMessage event) {
        int evenCode=event.getCode();

    }



    private void initView(View view){
        mTabLayout = view.findViewById(R.id.tabLayout);
        mViewPager = view.findViewById(R.id.viewPager);

        mAdapter = getAdapter();
        //给ViewPager设置适配器
        mViewPager.setAdapter(mAdapter);
        mViewPager.setSaveEnabled(false);
        //将TabLayout和ViewPager关联起来
        mTabLayout.setupWithViewPager(mViewPager);
        mViewPager.addOnPageChangeListener(new MyOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                         mCurrentFragment=mSaleReportFragment;
                        break;
                    case 1:
                        mCurrentFragment=mCustormReportFragment;
                        break;
                    case 2:
                        mCurrentFragment=mGoodReportFragment;
                        break;

                }
            }
        });

    }

    public ViewPagerAdapter getAdapter() {
        List<BaseFragmentInfo> datas = new ArrayList<>();
        //订单列表
        mSaleReportFragment=new SaleReportFragment();
        mCustormReportFragment=new CustormReportFragment();
        mGoodReportFragment=new GoodReportFragment();

        datas.add(new BaseFragmentInfo(1, "销售报表", mSaleReportFragment));
        datas.add(new BaseFragmentInfo(2, "客户报表", mCustormReportFragment));
        datas.add(new BaseFragmentInfo(3, "商品报表", mGoodReportFragment));

        mAdapter = new ViewPagerAdapter(getChildFragmentManager(), datas);
        return mAdapter;
    }


    private void removeFragment(Fragment fragment) {
        getChildFragmentManager().beginTransaction().remove(fragment).commitAllowingStateLoss();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        //todo 必须销毁它嵌套的相关fragment，不然下次添加会报错
        // todo因为子fragment没有销毁，还依附在当前fragment，不能再次被添加
        removeFragment(mSaleReportFragment); //移除嵌套的fragment
        removeFragment(mCustormReportFragment); //移除嵌套的fragment
        removeFragment(mGoodReportFragment); //移除嵌套的fragment
        mSaleReportFragment=null;
        mCustormReportFragment=null;
        mGoodReportFragment=null;

    }
}
